package com.mdxx.xczd.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

public class AutoScrollTextView extends TextView {
	private float textLength = 0f;  
    private float viewWidth = 0f;   
    private float tx = 0f;   
    private float ty = 0f;  
    private float temp_tx1 = 0.0f;  
    private float temp_tx2 = 0x0f;  
    private boolean isStarting = false;  
    private Paint paint = null;  
    private String text = "";  
  
    public AutoScrollTextView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  
   
    public void initScrollTextView(WindowManager windowManager, String text) {
        paint = this.getPaint();
        this.text = text;   
        textLength = paint.measureText(text);
        viewWidth = this.getWidth();
        if (viewWidth == 0) {  
            if (windowManager != null) { 
                Display display = windowManager.getDefaultDisplay();  
                viewWidth = display.getWidth(); 
            }  
        }  
        tx = textLength;  
        temp_tx1 = viewWidth + textLength;  
        temp_tx2 = viewWidth + textLength * 2; 
        ty = this.getTextSize() + this.getPaddingTop();  
    }  
  
    public void starScroll() {
        isStarting = true;
        this.invalidate();
    }  
   
    public void stopScroll() {  
        isStarting = false;  
        this.invalidate();
    }  
   
    @Override  
    protected void onDraw(Canvas canvas) {  
        if (isStarting) {
            canvas.drawText(text, temp_tx1 - tx, ty, paint);
            tx += 3;  
            if (tx >= temp_tx2) {                
                tx = temp_tx1 - viewWidth;  
            }  
            this.invalidate();
        }  
        super.onDraw(canvas);  
    }  
}  

